﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace GMusicBox
{
    internal class HotKeyTextBox : TextBox
    {
        public HotKeyTextBox()
        {
            this.ReadOnly = true;
        }

        bool ctrl, shift, alt;
        Keys key;
        public event EventHandler KeyChanged;

        public bool Ctrl
        {
            get { return ctrl; }
        }

        public bool Shift
        {
            get { return shift; }
        }

        public bool Alt
        {
            get { return alt; }
        }

        public Keys Key
        {
            get { return key; }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            ctrl = e.Control;
            shift = e.Shift;
            alt = e.Alt;
            key = e.KeyCode;
            RefreshText();
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            if (this.Text.EndsWith(" + ")) this.Text = "";
            else if (KeyChanged != null) KeyChanged(this, new EventArgs());
        }

        public void RefreshText()
        {
            string s = "", s1 = key.ToString();

            if ((int)key >= 48 && (int)key <= 57) s1 = s1.Substring(1);
            if (ctrl) s += "Ctrl + ";
            if (shift) s += "Shift + ";
            if (alt) s += "Alt + ";
            if (s1 == "ControlKey" || s1 == "ShiftKey" || s1 == "Menu") s1 = "";
            this.Text = s + s1;
            if (this.Text.EndsWith(" + ")) this.Text = "";
        }

        public void SetKey(string s)
        {
            string[] ss = s.Split(',');
            ctrl = Convert.ToBoolean(ss[0]);
            shift = Convert.ToBoolean(ss[1]);
            alt = Convert.ToBoolean(ss[2]);
            key = (Keys)int.Parse(ss[3]);
            RefreshText();
        }
    }
}
